{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1fa10Xz55WbN0P-xu7QXIWyIxJKykqQPY?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "### [OpenAI](https://openai.com/) - OpenAI LLM API call.\n"
      ],
      "metadata": {
        "id": "HxxOWz9dpsYj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Install required libraries"
      ],
      "metadata": {
        "id": "Y3axTI0sp5Hg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q -U langchain-openai==0.3.6"
      ],
      "metadata": {
        "id": "ShxTNxM5gqtr",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "b694d023-b44f-4d91-d9b0-13350845cbac"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m54.9/54.9 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m24.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.9/50.9 kB\u001b[0m \u001b[31m2.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Import related libraries"
      ],
      "metadata": {
        "id": "9jJ1vqs-p_Zx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Import Libraries\n",
        "from langchain_openai import ChatOpenAI"
      ],
      "metadata": {
        "id": "RL-3LsYogoH5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import getpass\n",
        "import os"
      ],
      "metadata": {
        "id": "GT55z5AkhyOW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Provide a OpenAI API key (Paid one)\n",
        "\n",
        "[OpenAI API Key Creation Link](https://platform.openai.com/api-keys)\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "F6UeDlrgqI2A"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yobvrD3glfd4",
        "outputId": "612f4237-0486-4ad4-a6a0-d3fa69f5a1e5"
      },
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "llm = ChatOpenAI(\n",
        "    model=\"gpt-4o\",\n",
        "    temperature=0.5\n",
        ")\n",
        "\n",
        "messages = [\n",
        "    (\n",
        "        \"system\",\n",
        "        \"You are a helpful assistant that explains problems step-by-step.\",\n",
        "    ),\n",
        "    (\"human\", \"Solve this problem step by step: {problem}\"),\n",
        "]"
      ],
      "metadata": {
        "id": "HHXlnTcxpi-8"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "ai_msg = llm.invoke(\"Provide me python code of sudoku\")\n",
        "print(ai_msg.content)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f4OZQ-kvqNl5",
        "outputId": "23bb7680-73bf-431c-ec4c-07da42e2b944"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Certainly! Below is a simple implementation of a Sudoku solver in Python using a backtracking algorithm. This code assumes you're working with a 9x9 Sudoku grid, where empty cells are represented by zeros.\n",
            "\n",
            "```python\n",
            "def print_board(board):\n",
            "    \"\"\"Helper function to print the Sudoku board.\"\"\"\n",
            "    for row in board:\n",
            "        print(\" \".join(str(num) if num != 0 else '.' for num in row))\n",
            "\n",
            "\n",
            "def is_valid(board, row, col, num):\n",
            "    \"\"\"Check if it's valid to place num at board[row][col].\"\"\"\n",
            "    # Check the row\n",
            "    for i in range(9):\n",
            "        if board[row][i] == num:\n",
            "            return False\n",
            "\n",
            "    # Check the column\n",
            "    for i in range(9):\n",
            "        if board[i][col] == num:\n",
            "            return False\n",
            "\n",
            "    # Check the 3x3 box\n",
            "    box_row_start = (row // 3) * 3\n",
            "    box_col_start = (col // 3) * 3\n",
            "    for i in range(box_row_start, box_row_start + 3):\n",
            "        for j in range(box_col_start, box_col_start + 3):\n",
            "            if board[i][j] == num:\n",
            "                return False\n",
            "\n",
            "    return True\n",
            "\n",
            "\n",
            "def solve_sudoku(board):\n",
            "    \"\"\"Solve the Sudoku puzzle using backtracking.\"\"\"\n",
            "    for row in range(9):\n",
            "        for col in range(9):\n",
            "            if board[row][col] == 0:  # Find an empty cell\n",
            "                for num in range(1, 10):  # Try numbers 1-9\n",
            "                    if is_valid(board, row, col, num):\n",
            "                        board[row][col] = num  # Tentatively place num\n",
            "\n",
            "                        if solve_sudoku(board):\n",
            "                            return True\n",
            "\n",
            "                        board[row][col] = 0  # Reset if num doesn't lead to a solution\n",
            "\n",
            "                return False  # Trigger backtracking\n",
            "\n",
            "    return True\n",
            "\n",
            "\n",
            "# Example Sudoku board (0 represents empty cells)\n",
            "sudoku_board = [\n",
            "    [5, 3, 0, 0, 7, 0, 0, 0, 0],\n",
            "    [6, 0, 0, 1, 9, 5, 0, 0, 0],\n",
            "    [0, 9, 8, 0, 0, 0, 0, 6, 0],\n",
            "    [8, 0, 0, 0, 6, 0, 0, 0, 3],\n",
            "    [4, 0, 0, 8, 0, 3, 0, 0, 1],\n",
            "    [7, 0, 0, 0, 2, 0, 0, 0, 6],\n",
            "    [0, 6, 0, 0, 0, 0, 2, 8, 0],\n",
            "    [0, 0, 0, 4, 1, 9, 0, 0, 5],\n",
            "    [0, 0, 0, 0, 8, 0, 0, 7, 9]\n",
            "]\n",
            "\n",
            "print(\"Original Sudoku Board:\")\n",
            "print_board(sudoku_board)\n",
            "\n",
            "if solve_sudoku(sudoku_board):\n",
            "    print(\"\\nSolved Sudoku Board:\")\n",
            "    print_board(sudoku_board)\n",
            "else:\n",
            "    print(\"No solution exists for the given Sudoku board.\")\n",
            "```\n",
            "\n",
            "This code will print the original Sudoku board and then attempt to solve it. If a solution exists, it will print the solved board; otherwise, it will indicate that no solution is possible. You can replace the `sudoku_board` with any other 9x9 grid to solve different puzzles.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "6S4n9GOir1Us"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}